#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include "../include/linkedlist.h"
#include "../include/constants.h"


/*search in a linked list*/
l_node *list_search_strkey( l_node *head, char *key )
{
    l_node* return_list;

    while(head!=NULL)
    {
        if (strcmp(head->data->f_name,key)==0)
        {
            return_list = list_insert_end(return_list, head->key, head->data);

        }
        head=head->next;
    }
    if( return_list!=NULL)
    {
        return return_list;
    }
    else return NULL;



}

l_node *list_search_prefix(l_node *head, char *key )
{
    l_node* return_list;
    l_node* current;
    current=head;
    int len=strlen(key);
    while(current!=NULL)
    {
        if (strncmp(current->data->f_name,key,len)==0)
        {
            return_list = list_insert_end(return_list, current->key, current->data);
        }
        current=current->next;
    }
    if( return_list!=NULL)
    {
        return return_list;
    }
    else return NULL;
}


l_node * list_insert_start(l_node *head, void *key, fdesc* data)
{
    l_node *start;
    start = (l_node*)malloc(sizeof(l_node));
    start->data =data;
    start->key=key;
    start->next=NULL;
    head=start;
    return head;

}

/*------------inserting at the end------------------------*/
l_node *list_insert_end( l_node *head,void *key,fdesc *data)
{
    l_node *current, *prev, *new_node;
    char *temp;
    if (head == NULL)
    {
        return list_insert_start(head,key,data);
    }
    prev = NULL;
    current = head;

    while(current->next!= NULL)
    {
        current = current->next;

    }
    new_node = (l_node*)malloc(sizeof(l_node));
    new_node->data =data;
    new_node->key=key;
    new_node->next = NULL;
    current->next=new_node;


    return head;
}


//deleting a node in a linked list---------------------------------

l_node *list_delete_node( l_node *head, fdesc* fd)
{

    l_node *prev, *current,*new_head;
    if (head == NULL)
    {
        return NULL;
    }
    prev = NULL;
    current = head;
    while(current != NULL)
    {
        if (current->data==fd)
        {
            if (prev == NULL)
            {
                new_head = head->next;
                free(head);
                return new_head;
            }
            prev->next=current->next;
            free(current);
            break;
        }
        prev = current;
        current = current->next;
    }
    return head;
}

void print_list(l_node* head)
{
    l_node *current = head;
    while (current != NULL )
    {
        printf("%s - > ", (char*)current->data->f_name);
        current = current->next;
    }
}

l_node *list_insert(l_node *head,void *data)
{
    l_node *current, *prev, *new_node;
    if (head == NULL)
    {
        l_node *start;
        start = (l_node*)malloc(sizeof(l_node));
        start->data =data;
        start->next=NULL;
        head=start;
        return head;
    }
    prev = NULL;
    current = head;
    while(current->next != NULL)
    {
        current = current->next;
    }
    new_node = (l_node*)malloc(sizeof(l_node));
    new_node->data =data;
    new_node->next = NULL;
    current->next=new_node;

    return head;
}





